package com.threebrothers.lib_log

import com.threebrothers.lib_log.logger.EmailLogger
import com.threebrothers.lib_log.logger.FileLogger
import com.threebrothers.lib_log.logger.ILogger
import com.threebrothers.lib_log.logger.LoggerCatLogger
import com.threebrothers.lib_log.logger.NetLogger

class LoggerManager private constructor(
    private val TAG:String,
    private val level:LoggerLevel,
    private val loggerStrategy : LoggerStrategy,
    private val isDebug:Boolean,
    private val saveUrl : String,
){
    //**********策略者模式*******************
    //3:根据不同的策略创建不同的logger
    private var logger : ILogger = when(loggerStrategy){
        LoggerStrategy.LOGCAT ->LoggerCatLogger()
        LoggerStrategy.FILE -> FileLogger()
        LoggerStrategy.EMAIL -> EmailLogger()
        LoggerStrategy.NET -> NetLogger()
    }



    //4:设置属性
    init {
        logger.setLoggerLevel(level)
        logger.setLogTAG(TAG)
        logger.setDebug(isDebug)
        logger.setSaveUrl(saveUrl)
    }
    //4:提供打印日志方法
    fun v(TAG: String,log:String){
        logger.v(TAG,log)
    }
    fun d(TAG: String,log:String){
        logger.d(TAG,log)
    }
    fun i(TAG: String,log:String){
        logger.i(TAG,log)
    }
    fun w(TAG: String,log:String){
        logger.w(TAG,log)
    }
    fun e(TAG: String,log:String){
        logger.e(TAG,log)
    }





    //2:次构造，Builder作为参数
    constructor(builder : Builder) : this(
        builder.TAG,
        builder.level,
        builder.loggerStrategy,
        builder.isDebug,
        builder.saveUrl
    )


    //**************构建者模式**************
    //1定义构建类

    class Builder{
        var TAG:String = "默认TAG"
        var level : LoggerLevel = LoggerLevel.Verbose
        var loggerStrategy  : LoggerStrategy = LoggerStrategy.LOGCAT
        var isDebug : Boolean = true
        var saveUrl : String = ""

        fun setTAg(TAG:String) : Builder{
            this.TAG = TAG
            return this
        }

        fun setLoggerLevel(level : LoggerLevel) : Builder{
            this.level = level
            return  this
        }
        fun setLoggerStrategy(loggerStrategy : LoggerStrategy) : Builder{
            this.loggerStrategy = loggerStrategy
            return  this
        }
        fun setDebug(isDebug : Boolean) : Builder{
            this.isDebug = isDebug
            return  this
        }
        fun setSaveUrl(url : String) : Builder{
            this.saveUrl = url
            return  this
        }
        /**
         * 构建对象
         */
        fun build() : LoggerManager{
            return LoggerManager(this)
        }
    }
}